/**
 * A simple caps lock detection plugin for jQuery. Inspired by the "24 ways:
 * Capturing Caps Lock" article by Stuart Landridge
 * (http://24ways.org/2007/capturing-caps-lock).
 *
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2008 Michael J. I. Jackson
 * @license     http://www.opensource.org/licenses/mit-license.php
 * @version     1.0.0
 */

/**
 * Sets up a keypress listener on the selected elements that can be used to
 * handle situations where the caps lock may be activated (for example, on a
 * password field). The callback will be called in the scope of the current
 * element with one parameter - a boolean indicating whether or not the caps
 * lock is on.
 *
 * @param   Function    cb      The callback function to call
 * @return  jQuery              The jQuery object
 * @public
 */
jQuery.fn.caps = function(cb) {
    return this.keypress(function(e) {
        var w = e.which ? e.which : (e.keyCode ? e.keyCode : -1);
        var s = e.shiftKey ? e.shiftKey : (e.modifiers ? !!(e.modifiers & 4) : false);
        var c = ((w >= 65 && w <= 90) && !s) || ((w >= 97 && w <= 122) && s);
        cb.call(this, c);
    });
};

(function($){
    var $body = $(document.body),
        $menu = $('nav.base ul'),
        $content = $('div.content');

    $.Ajaxy.configure({
        root_url: app.base_url||'',
        Controllers: {
            '_generic': {
                request: function(){
                    $body.addClass('loading');
                    return true;
                },
                response: function(){
                    var Ajaxy = $.Ajaxy,
                        Data = this.State.Response.data,
                        Title = Data.title||false; // if we have a title in the response JSON
                        //state = this.state||'unknown';

                    if ( !Title && this.state||false ) { // if not use the state as the title
                        Title = 'jQuery Ajaxy - '+this.state;
                    }

                    if (Title) { // if we have a new title use it
                        document.title = Title;
                    }

                    $body.removeClass('loading'); // Loaded
                    return true;
                },
                error: function(){
                    var Ajaxy = $.Ajaxy,
                        Data = this.State.Error.data||this.State.Response.data,
                        Error = Data.error||Data.responseText||'Unknown Error.',
                        ErrorMessage = Data.content||error;
                        //state = this.state||'unknown';

                    // Log what is happening
                    window.console.error('$.Ajaxy.Controllers._generic.error', [this, arguments], ErrorMessage);
                    $body.removeClass('loading'); // Loaded

                    return true;
                }
            },
            'page': {
                classname: 'ajaxy-page',
                matches: /^\/?/,
                request: function(){
                    //var Ajaxy = $.Ajaxy;
                    $menu.find('.active').removeClass('active'); // Adjust Menu
                    $content.stop(true, true).fadeOut(400); // Hide Content
                    return true;
                },
                response: function(){
                    //var Ajaxy = $.Ajaxy;
                    var Action = this,
                        State = Action.State,
                        Data = State.Response.data,
                        Query = (State.raw.state.length == 1) ? ':first' : ':has(a[href*="'+State.raw.state+'"])';
                        //state = this.state;

                    $menu
                        .children(Query)
                        .addClass('active')
                        .siblings('.active')
                        .removeClass('active');

                    // Show Content
                    $content
                        .html(Data.content)
                        .fadeIn(400, function() {
                            Action.documentReady($content);
                        });

                    return true;
                }
            }
        }
    });
})(jQuery);
